package com.huihuiyun.huihuiyun.server.service;

import com.huihuiyun.huihuiyun.server.domain.HhyMemberAddress;
import com.huihuiyun.huihuiyun.server.repository.HhyMemberAddressRepository;
import com.mark.util.CommonUtil;
import com.mark.util.JaxrsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Vector;

@Component
@Path("/hhyMemberAddresss")
@Slf4j
public class HhyMemberAddressService {
    @Autowired
    private HhyMemberAddressRepository hhyMemberAddressRepository;
    @Context
    private HttpServletResponse httpServletResponse;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public HhyMemberAddress post(HhyMemberAddress hhyMemberAddress) {
        try {
            hhyMemberAddress.id = CommonUtil.createRandomId();
            hhyMemberAddress = hhyMemberAddressRepository.save(hhyMemberAddress);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.CREATED.getStatusCode());
            return hhyMemberAddress;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @DELETE
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public void delete(@PathParam("id") String id) {
        try {
            hhyMemberAddressRepository.delete(id);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.NO_CONTENT.getStatusCode());
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public HhyMemberAddress put(HhyMemberAddress hhyMemberAddress) {
        try {
            if (CommonUtil.isEmpty(hhyMemberAddress.id)) {
                hhyMemberAddress.id = CommonUtil.createRandomId();
            }
            hhyMemberAddress = hhyMemberAddressRepository.save(hhyMemberAddress);
            return hhyMemberAddress;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public HhyMemberAddress findOne(@PathParam("id") String id) {
        try {
            HhyMemberAddress hhyMemberAddress = hhyMemberAddressRepository.findOne(id);
            if (hhyMemberAddress == null) {
                JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.NO_CONTENT.getStatusCode());
                return null;
            } else {
                return hhyMemberAddress;
            }
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<HhyMemberAddress> findAll() {
        try {
            List<HhyMemberAddress> hhyMemberAddressList = hhyMemberAddressRepository.findAll();
            return hhyMemberAddressList;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findAll")
    public Page<HhyMemberAddress> findAll(@QueryParam("pageNumber") int pageNumber, @QueryParam("pageSize") int pageSize) {
        try {
            PageRequest pageRequest = new PageRequest(pageNumber, pageSize);
            Page<HhyMemberAddress> page = hhyMemberAddressRepository.findAll(pageRequest);
            return page;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/branch")
    public void post(List<HhyMemberAddress> hhyMemberAddressList) {
        try {
            hhyMemberAddressRepository.save(hhyMemberAddressList);
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    public List<HhyMemberAddress> findAllByMemberId(String memberId) {
        try {
            List<HhyMemberAddress> hhyMemberAddressList = hhyMemberAddressRepository.findAllByMemberId(memberId);
            return hhyMemberAddressList;
        } catch (Exception e) {
            log.error("", e);
            return new Vector<>();
        }
    }

    public Page<HhyMemberAddress> findAllByMemberId(String memberId, int pageNumber, int pageSize) {
        try {
            PageRequest pageRequest = new PageRequest(pageNumber, pageSize);
            Page<HhyMemberAddress> hhyMemberAddressPage = hhyMemberAddressRepository.findAllByMemberId(memberId, pageRequest);
            return hhyMemberAddressPage;
        } catch (Exception e) {
            log.error("", e);
            return null;
        }
    }
}
